<?php
namespace app\admin\controller;
use app\common\controller\Adminbase;
use app\admin\model\AdminUser;
use think\Db;
use app\admin\model\Product as ProductModel;
use app\admin\model\Danwei as DanweiModel;
use app\admin\model\Custom as CustomModel;

class Product extends Adminbase
{
    public function index()
    {
        if ($this->request->isAjax()) {
            $page = $this->request->param('page');
            $limit = $this->request->param('limit');
            if($this->request->param('filter')){
                $filter = $this->request->param('filter');
                $filter = json_decode($filter,true);
                foreach($filter as $k=>$row){
                    $where[] = [$k,'like','%'.trim($row).'%'];
                }
            }
            $offset = ($page - 1)*$limit;
            $result = ProductModel::where('deleted',0)->where($where)->limit($offset,$limit)->select();
            foreach($result as $k=>$row){
                $result[$k]['admin'] = AdminUser::getAdmin($row['admin_id'],'nickname');
                $result[$k]['danwei'] = DanweiModel::getDanwei($row['danwei_id']);
            }
            $count = ProductModel::where('deleted',0)->where($where)->count();
            $result = array("code" => 0, 'count'=>$count,"data" => $result);
            return json($result);
        }
        return $this->fetch();
    }

    public function add()
    {
        $danwei = DanweiModel::where('deleted',0)->select();
        $this->assign('danwei',$danwei);
        //自定义字段
        $customList = CustomModel::where(['type'=>'product','deleted'=>0])->select();
        $custom = [];
        foreach($customList as $k=>$row){
            $value = Db::name('gd_custom_'.$row['field_type'])->where('custom_id',$row['id'])->find();
            if($row['field_type'] == 'radio' || $row['field_type'] == 'checkbox'){
                $value['select'] = explode(',',$value['select']);
            }
            if($value['hidden']){
                $value['hidden'] = exchangeHidden($value['hidden'],'product');
            }
            $custom[] = [
                'title'=>$row['title'],
                'field_type'=>$row['field_type'],
                'attribute'=>$value
            ];
        }
        $admin = AdminUser::find(session('admin_user_auth')['uid']);
        $this->assign('custom',$custom);
        $this->assign('admin',$admin);
        return $this->fetch();
    }

    public function edit()
    {
        $id = $this->request->param('id');
        $product = ProductModel::find($id);
        $danwei = DanweiModel::where('deleted',0)->select();
        //自定义字段
        $customList = CustomModel::where(['type'=>'product','deleted'=>0])->select();
        $custom = [];
        foreach($customList as $k=>$row){
            $value = Db::name('gd_custom_'.$row['field_type'])->where('custom_id',$row['id'])->find();
            if($row['field_type'] == 'radio' || $row['field_type'] == 'checkbox'){
                $value['select'] = explode(',',$value['select']);
            }
            if($value['hidden']){
                $value['hidden'] = exchangeHidden($value['hidden'],'product');
            }
            $custom[] = [
                'title'=>$row['title'],
                'field_type'=>$row['field_type'],
                'attribute'=>$value
            ];
        }
        //自定义字段值
        $values = Db::name('gd_extra_field')->where(['target_id'=>$id,'target'=>'product'])->select();
        // dg($values);
        foreach($custom as $k=>$row){
            if($values[$k]['name'] == 'extra_'.($k+1)){
                $custom[$k]['value'] = $values[$k]['value'];
            }
        }

        $admin = AdminUser::find(session('admin_user_auth')['uid']);
        $this->assign('admin',$admin);
        $this->assign('danwei',$danwei);
        $this->assign('product',$product);
        $this->assign('customList',$customList);
        $this->assign('custom',$custom);
        return $this->fetch();
    }

    public function editPost()
    {
        $data = $this->request->param();
        $extra = $data['extra'];
        $field_type = $data['field_type'];
        $extra_title = $data['extra-title'];
        unset($data['extra']);
        unset($data['field_type']);
        unset($data['extra-title']);
        unset($data['mobile']);
        unset($data['nickname']);
        unset($data['username']);
        unset($data['danwei']);
        if(!isset($data['id'])){   //新增
            $check = ProductModel::where(['nums'=>$data['nums'],'deleted'=>0])->find();
            if(!empty($check)){
                $this->error('产品编号重复了');
            }
            $insertId = ProductModel::insertGetId([
                'nums'=>$data['nums'],
                'title'=>$data['title'],
                'guige'=>$data['guige'],
                'danwei_id'=>$data['danwei_id'],
                'admin_id'=>session('admin_user_auth')['uid'],
                'addtime'=>date('Y-m-d H:i:s')
            ]);
            foreach($field_type as $k=>$row){
                Db::name('gd_extra_field')->insert([
                    'target'=>'product',
                    'target_id'=>$insertId,
                    'type_field'=>$row,
                    'title'=>$extra_title[$k],
                    'value'=>$data['extra_'.($k+1)],
                    'name'=>'extra_'.($k+1),
                ]);
            }
        }else{
            $extraList = Db::name('gd_extra_field')->where(['target'=>'product','target_id'=>$data['id']])->select();
            foreach($extra_title as $k=>$row){
                foreach($extraList as $kk=>$row2){
                    if('extra_'.($k+1) == $row2['name']){  //这个表单已存在
                        $extraDetail = Db::name('gd_extra_field')->where(['name'=>'extra_'.($k+1),'target_id'=>$data['id'],'target'=>'product'])->find();
                        Db::name('gd_extra_field')->where('id',$extraDetail['id'])->update([
                            'value'=>$data['extra_'.($k+1)],
                            'mdtime'=>date('Y-m-d H:i:s')
                        ]);
                        unset($extra_title[$k]);
                    }
                }
            }
            // foreach($extra as $k=>$row){
            //     foreach($extraList as $kk=>$row2){
            //         if('extra_'.($k+1) == $row2['name']){  //这个表单已存在
            //             $extraDetail = Db::name('gd_extra_field')->where(['name'=>'extra_'.($k+1),'target_id'=>$data['id'],'target'=>'product'])->find();
            //             Db::name('gd_extra_field')->where('id',$extraDetail['id'])->update([
            //                 'value'=>$row,
            //                 'mdtime'=>date('Y-m-d H:i:s')
            //             ]);
            //             unset($extra_title[$k]);
            //         }
            //     }
            // }
            foreach($extra_title as $k=>$row){
                Db::name('gd_extra_field')->insert([
                    'target'=>'product',
                    'target_id'=>$data['id'],
                    'type_field'=>$field_type[$k],
                    'title'=>$extra_title[$k],
                    'value'=>$data['extra_'.($k+1)],
                    'name'=>'extra_'.($k+1),
                ]);
            }
            $data['mdtime'] = date('Y-m-d H:i:s');
            ProductModel::where('id',$data['id'])->update([
                'title'=>$data['title'],
                'guige'=>$data['guige'],
                'danwei_id'=>$data['danwei_id'],
                'admin_id'=>session('admin_user_auth')['uid'],
                'mdtime'=>date('Y-m-d H:i:s')
            ]);
        }
        $this->success('添加成功');
    }
    public function delete()
    {
        $data = $this->request->param();
        ProductRouteModel::where('id',$data['id'])->update(['deleted'=>1]);
        $this->success('删除成功',url('index'));
    }
}